package com.hainiu.cat.web.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * create by biji.zhao on 2021/6/4
 */
@Configuration
public class DirectRabbitConfig {

    /**
     * 用于绑定到直连交换机的队列
     */
    public static final String TEST_DIRECT_QUEUE = "TestDirectQueue";

    /**
     * 声明直连交换机
     */
    public static final String TEST_DIRECT_EXCHANGE = "TestDirectExchange";

    /**
     * 声明关于直连交换机TEST_DIRECT_EXCHANGE的routing-key :
     */
    public static final String TEST_DIRECT_ROUTING = "TestDirectRouting";

    /**
     * 声明直连交换机 这个交换机不绑定任何队列
     */
    public static final String TEST_DIRECT_EXCHANGE_NO_HAVE__QUEUE = "LonelyExchange";

    /**
     * 队列 起名：TestDirectQueue
     * @return 抽象队列
     */
    @Bean
    public Queue testDirectQueue() {
        //true 是否持久
        return new Queue(TEST_DIRECT_QUEUE,true);
    }

    /**
     * Direct交换机 起名：TestDirectExchange  直连交换机,需要双方遵守routingKey
     * @return
     */
    @Bean
    DirectExchange testDirectExchange() {
        return new DirectExchange(TEST_DIRECT_EXCHANGE);
    }

    /**
     * 绑定  将队列和交换机绑定, 并设置用于匹配键：TestDirectRouting
     * @return
     */
    @Bean
    Binding bindingDirect() {
        return BindingBuilder.bind(testDirectQueue()).to(testDirectExchange()).with(TEST_DIRECT_ROUTING);
    }

    /**
     * Direct交换机 起名：LonelyDirectExchange  模拟生产者发送消息时找不到交换机的情形
     * @return
     */
    @Bean
    DirectExchange testExchangeNoQueueExit() {
        return new DirectExchange(TEST_DIRECT_EXCHANGE_NO_HAVE__QUEUE);
    }
}
